package com.haochang.chunkvod.controller.activity.cash;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.SaleUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.cash.ShiftQueryAdapter;
import com.haochang.chunkvod.model.cash.BillInfo;
import com.haochang.chunkvod.model.cash.CashData;
import com.haochang.chunkvod.model.cash.PrintEnum;
import com.haochang.chunkvod.model.cash.PrintInfo;
import com.haochang.chunkvod.model.cash.VehicleData;
import com.haochang.chunkvod.model.cash.VehicleInfo;
import com.haochang.chunkvod.tools.vod.thread.PrintThread;
import com.haochang.chunkvod.tools.vod.thread.ThreadPoolUtil;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

/**
 *@author qintao
 *created at 2016/7/26 11:59
 * 交班查询
 */

public class ShiftQueryActivity extends BaseActivity
{
	private TitleView titleView;
	private BaseTextView tv_operateDate;
	private BaseTextView tv_vehicle;
	private BaseTextView tv_vehicleNum;
	private BaseTextView tv_totalValue;
	private BaseTextView tv_receivableValue;
	private BaseTextView tv_pendValue;
	private BaseTextView tv_receivedValue;
	private Button btn_collect;
	private BaseListView listView;
	private ShiftQueryAdapter adapter;
	private List<BillInfo> vehicleInfoList = new ArrayList<BillInfo>();
	private VehicleData vehicleData;
	private VehicleInfo vehicle;
	private CashData cashData;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public void receiveParam()
	{

	}

	@Override
	public void initData()
	{
		vehicleData = new VehicleData(ShiftQueryActivity.this);
		cashData = new CashData(ShiftQueryActivity.this);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_shift_query);
		titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle("交班查询").setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
			}

			@Override
			public void onDoneClick()
			{
				List<String> list = new ArrayList<String>();
				list.add("退出登录");
				int location[] = new int[2];
				ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
				img.getLocationOnScreen(location);
				int x = location[0];
				final VodPopWindow pop = new VodPopWindow(ShiftQueryActivity.this, list);
				pop.showPopupWindow(titleView, x);
				pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener()
				{
					@Override
					public void onSelectItem(int position)
					{
						switch (position)
						{
						case 0:
							loginOut();
							break;
						default:
							break;
						}
					}
				});
			}
		});
		tv_operateDate = (BaseTextView) findViewById(R.id.tv_operateDate);
		tv_vehicle = (BaseTextView) findViewById(R.id.tv_vehicle);
		tv_vehicleNum = (BaseTextView) findViewById(R.id.tv_vehicleNum);
		tv_totalValue = (BaseTextView) findViewById(R.id.tv_totalValue);
		tv_receivableValue = (BaseTextView) findViewById(R.id.tv_receivableValue);
		tv_pendValue = (BaseTextView) findViewById(R.id.tv_pendValue);
		tv_receivedValue = (BaseTextView) findViewById(R.id.tv_receivedValue);
		tv_totalValue.setOnClickListener(mOnBaseClickListener);
		btn_collect = (Button) findViewById(R.id.btn_collect);
		btn_collect.setOnClickListener(mOnBaseClickListener);
		listView = (BaseListView) findViewById(R.id.listView);
		adapter = new ShiftQueryAdapter(ShiftQueryActivity.this, R.layout.item_shift_query_list_layout, vehicleInfoList);
		listView.setAdapter(adapter);
		vehicleData.requestShiftQuery(new VehicleData.IShiftQueryListener()
		{
			@Override
			public void onRequestSuccess(VehicleInfo vehicleInfo)
			{
				if (vehicleInfo != null)
				{
					vehicle = vehicleInfo;
					tv_totalValue.setText(vehicleInfo.getBill_count() + "单");
					tv_receivableValue.setText(SaleUtils.getSaleString(vehicleInfo.getReceivable_fee()));
					tv_pendValue.setText(SaleUtils.getSaleString(vehicleInfo.getGuadan_fee()));
					tv_receivedValue.setText(SaleUtils.getSaleString(vehicleInfo.getPaid_fee()));
					tv_operateDate.setText(String.format("营业时间：%s", vehicleInfo.getBusiness_date()));
					tv_vehicle.setText(String.format("班次：%s", vehicleInfo.getShiftName()));
					tv_vehicleNum.setText(String.format("共%d班", vehicleInfo.getShift_count()));
					if (!CollectionUtils.isEmpty(vehicleInfo.getBillInfoList()))
					{
						vehicleInfoList.clear();
						vehicleInfoList.addAll(vehicleInfo.getBillInfoList());
						adapter.replaceAll(vehicleInfoList);
					}
				}
			}
		});
	}

	private OnBaseClickListener mOnBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.btn_collect:
				cashData.requestCollectionInfo(new CashData.IPrintInfoListener()
				{
					@Override
					public void onRequestSuccess(JSONObject jsonObject)
					{
						if (jsonObject != null)
						{
							PrintInfo printInfo = new PrintInfo();
							printInfo.setType(PrintEnum.PRINT_COLLECT.getName());
							printInfo.setContent(jsonObject.toString());
							ThreadPoolUtil.getInstanceSingleTaskExecutor().submit(new PrintThread(PrintEnum.PRINT_COLLECT, printInfo));
						}
					}

					@Override
					public void onRequestFail(int errno, String errMsg)
					{
						ToastUtils.showText(ShiftQueryActivity.this, "获取汇总列印打印信息失败");
					}
				});

				break;
			case R.id.tv_totalValue:
				Intent intent = new Intent(ShiftQueryActivity.this, ShiftDetailsActivity.class);
				startActivity(intent);
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
	}
}
